//
//  DALabel.m
//  DAKitTest
//
//  Created by DreamArts on 16/3/29.
//  Copyright © 2016年 DreamArts. All rights reserved.
//

#import "DALabel.h"

@implementation DALabel

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        //TODO:做一些DALabel的默认设置
        self.style = DALabelStyleDefault;
    }
    return self;
}

#pragma mark - 枚举设置
- (void)setStyle:(DALabelStyle)style{
    switch (style) {
        case DALabelStyleDefault:
            self.font = [UIFont systemFontOfSize:14];
            self.textColor = [UIColor darkGrayColor];
            break;
        case DALabelStyleBlack:
            self.font = [UIFont systemFontOfSize:14];
            self.textColor = [UIColor blackColor];
            break;
        case DALabelStyleWhite:
            self.font = [UIFont systemFontOfSize:14];
            self.textColor = [UIColor whiteColor];
            break;
            
        default:
            break;
    }
}

#pragma mark - override
//重写textRectForBounds方法
- (CGRect)textRectForBounds:(CGRect)bounds
     limitedToNumberOfLines:(NSInteger)numberOfLines {
    CGRect textRect = [super textRectForBounds:bounds limitedToNumberOfLines:numberOfLines];
    switch (self.verticalAlignment) {
        case VerticalAlignmentTop:
            textRect.origin.y = bounds.origin.y;
            break;
        case VerticalAlignmentBottom:
            textRect.origin.y = bounds.origin.y + bounds.size.height - textRect.size.height;
            break;
        case VerticalAlignmentMiddle:
            // do nothing
        default:
            textRect.origin.y = bounds.origin.y + (bounds.size.height - textRect.size.height) / 2.0;
    }
    return textRect;
}

//重写drawTextInRect方法
- (void)drawTextInRect:(CGRect)requestedRect {
    CGRect actualRect = [self textRectForBounds:requestedRect limitedToNumberOfLines:self.numberOfLines];
    [super drawTextInRect:actualRect];
}

//重写setText方法
- (void)setText:(id)text fitToContent:(BOOL)fit{
    if([text isKindOfClass:[NSNumber class]]){//当参数为NSNumber类型的时候
        NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
        NSString *aText = [numberFormatter stringFromNumber:text];//将text转换成NSString类型
        [super setText:aText];
    }else{
        [super setText:text];
    }
    if(fit){//自适应高度
        [self fitToContentText];
    }
}
#pragma mark - 自适应高度
- (void)fitToContentText{
    self.numberOfLines = 0;
    self.lineBreakMode = NSLineBreakByWordWrapping;
    CGSize size = [self sizeThatFits:CGSizeMake(self.frame.size.width, MAXFLOAT)];
    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, size.height);
}

- (void)setHeightForLabel{
    [self fitToContentText];
}
#pragma mark - 添加长按手势并添加动画
- (void)addLongPanWithAnimation:(BOOL)animation
{
    if (animation) {
        self.userInteractionEnabled = YES;
        UILongPressGestureRecognizer *longPGR = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPGRAction:)];
        [self addGestureRecognizer:longPGR];
    }
}

// 动画
- (void)longPGRAction:(UILongPressGestureRecognizer *)lonP{
    // 旋转动画 rotation
    CAKeyframeAnimation *keyAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];
    float top = M_PI / 18;
    float bom = -M_PI / 18;
    keyAnimation.values = @[@(top),@(bom),@(0), @(top)];
    keyAnimation.repeatCount = NSUIntegerMax;
    keyAnimation.duration = 0.3;
    [self.layer addAnimation:keyAnimation forKey:@"key"];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 停止动画
        [self.layer removeAnimationForKey:@"key"];
        
    });
}


#pragma mark - 想要自己定义长按动画
- (void)customLongPanAction:(SEL)action target:(id)target animation:(BOOL)animation
{
    // 判断是否开启动画
    if (animation) {
        self.userInteractionEnabled = YES;
        UILongPressGestureRecognizer *PGR = [[UILongPressGestureRecognizer alloc] initWithTarget:target action:action];
        [self addGestureRecognizer:PGR];
    } else{
        self.userInteractionEnabled = NO;
    }
}

#pragma mark - 拖拽
- (BOOL)dragEnabled
{
    return NO;  // 默认不开启
}

- (void)setDragEnabled:(BOOL)dragEnabled
{
    if (dragEnabled) {
        self.userInteractionEnabled = YES;
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panchAction:)];
        [self addGestureRecognizer:pan];
    } else{
        self.userInteractionEnabled = NO;
    }
}

// 拖拽方法
- (void)panchAction:(UIPanGestureRecognizer *)pan{
    
    DALabel *label = (DALabel *)pan.view;
    CGPoint p = [pan translationInView:label];
    [label setTransform:CGAffineTransformTranslate(label.transform, p.x, p.y)];
    [pan setTranslation:CGPointZero inView:label];
}

@end
